package com.engine.qz.util.response;
import weaver.general.BaseBean;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.io.Serializable;

public class ReturnResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private long code;
    private T data;
    private String msg;

    public ReturnResult() {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMessage();
    }

    /**
     * @param code
     * @param msg
     * @param data
     */

    public ReturnResult(long code, String msg, T data) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    /**
     * 返回 code 和 data
     *
     * @param data
     */
    public ReturnResult(T data) {
        this.code = ResultCode.SUCCESS.getCode();
        this.data = data;
    }

    /**
     * 返回 code 和 msg
     *
     * @param code
     * @param msg
     */
    public ReturnResult(long code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }


    public ReturnResult Msg(String msg) {
        this.setMsg(msg);
        return this;
    }


    public ReturnResult Code(long code) {
        this.setCode(code);
        return this;
    }


    public ReturnResult Data(T data) {
        this.setData(data);
        return this;
    }

    /**
     * 返回默认状态
     */
    public static <T> ReturnResult<T> successed(User user) {
        return new ReturnResult<>(ResultCode.SUCCESS.getCode(), SystemEnv.getHtmlLabelName(ResultCode.SUCCESS.getLabelId(), user.getLanguage()));
    }

    /**
     * 成功返回
     *
     * @param data
     * @return
     */
    @Deprecated
    public static <T> ReturnResult<T> successed(T data) {

        return new ReturnResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 多语言响应
     *
     * @param user
     * @param data
     * @param <T>
     * @return
     */
    public static <T> ReturnResult<T> successed(User user, T data) {
        return new ReturnResult<>(ResultCode.SUCCESS.getCode(), SystemEnv.getHtmlLabelName(ResultCode.SUCCESS.getLabelId(), user.getLanguage()), data);
    }

    /**
     * 异常处理
     *
     * @param e
     * @param <T>
     * @return
     */
    public static <T> ReturnResult<T> exceptionHandle(Exception e) {
        new BaseBean().writeLog(e);
        return new ReturnResult<>(ResultCode.FAILED.getCode(), e.getMessage());
    }

}
